package lumingweihua.future.cn.lumingweihua.ui.mine;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.zhusx.core.adapter.Lib_BaseRecyclerAdapter;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Densitys;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.release.SelectMapSiteActivity;
import lumingweihua.future.cn.lumingweihua.ui.BaseAppFragment;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.PushData;
import lumingweihua.future.cn.lumingweihua.ui.order.RushOrderDetailActivity;

/**
 * todo 我的抢单
 * Created by z1986 on 2018/3/17.
 */

public class MineRushActivity extends BaseCompatActivity {
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.layout_content)
    LinearLayout layoutContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_rush);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            String[] names = new String[]{"已通过","待审核",  "已拒绝"};
            RushFragment[] fragments = new RushFragment[names.length];

            @Override
            public Fragment getItem(int position) {
                if (fragments[position] == null) {
                    fragments[position] = new RushFragment();
                }
                switch (position) {
                    case 0:
                        fragments[position].setIndex(2);
                        break;
                    case 1:
                        fragments[position].setIndex(1);
                        break;
                    case 2:
                        fragments[position].setIndex(3);
                        break;
                }
                return fragments[position];
            }

            @Override
            public int getCount() {
                return names.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return names[position];
            }
        });
        viewPager.setOffscreenPageLimit(2);
        tabLayout.setupWithViewPager(viewPager);
    }

    @OnClick({R.id.layout_search})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_search:
                startActivity(new Intent(v.getContext(), SelectMapSiteActivity.class));
                break;
        }
    }

    public static class RushFragment extends BaseAppFragment {
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;

        Lib_BaseRecyclerAdapter<PushData> adapter;
        int status;
        LoadData<List<PushData>> loadData;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_mine_push, container, false);
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            ButterKnife.bind(this, view);
            init();
        }

        private void init() {
            loadData = new LoadData<>(LoadData.Api.个人中心我的抢单, this);
            recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                    outRect.set(0, _Densitys.dip2px(view.getContext(), 10), 0, 0);
                }
            });
            recyclerView.setAdapter(adapter = new Lib_BaseRecyclerAdapter<PushData>() {
                @Override
                protected void __bindViewHolder(_ViewHolder viewHolder, int i, final PushData s) {
                    viewHolder.setText(R.id.tv_num, "货单号: " + s.good_num);
                    viewHolder.setText(R.id.tv_date, s.use_time);
                    viewHolder.setText(R.id.tv_name, s.car_name + " " + s.license);
                    viewHolder.setText(R.id.tv_count, s.rough_weight + "吨");
                    viewHolder.setText(R.id.tv_type, s.type);
                    viewHolder.setText(R.id.tv_fromCity, s.loading);
                    viewHolder.setText(R.id.tv_toCity, s.unload);
                    viewHolder.setText(R.id.tv_weight, "共" + s.weight + "吨");
                    viewHolder.setText(R.id.tv_rough_weight, s.rough_weight + "吨");
                    viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(view.getContext(), RushOrderDetailActivity.class).putExtra(_EXTRA_String_ID, s.id));
                        }
                    });
                }

                @Override
                protected int __getLayoutResource(int i) {
                    return R.layout.item_list_mine_rush;
                }

                @Override
                public void onBindViewHolder(_ViewHolder holder, int position) {
                    super.onBindViewHolder(holder, position);
                    if (position == getItemCount() - 1 && getItemCount() % LoadData.PAGE_ITME == 0) {
                        loadData._reLoadData(false);
                    }
                }
            });
            loadData._setOnLoadingListener(new LoadingHelper<List<PushData>>(recyclerView, loadData) {
                @Override
                public void __onComplete(HttpRequest<Object> request, IHttpResult<List<PushData>> data) {
                    if (request.isRefresh) {
                        adapter._setItemToUpdate(data.getData());
                    } else {
                        adapter._addItemToUpdate(data.getData());
                    }
                }
            });
            loadData._loadData(status);
        }

        public void setIndex(int status) {
            this.status = status;
        }
    }
}
